package cn.edu.sdjzu.xg.bysj.dao;

import cn.edu.sdjzu.xg.bysj.domain.ProfTitle;
import util.Condition;
import util.Pagination;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;
import java.util.TreeSet;

public final class ProfTitleDao {
    private static ProfTitleDao profTitleDao = new ProfTitleDao();


    private ProfTitleDao() {
    }

    public static ProfTitleDao getInstance() {
        return profTitleDao;
    }

    public static Collection<ProfTitle> findAll(List conditionList, Pagination pagination, Connection connection) throws SQLException {
        //创建集合类对象，用来保存所有记录代表的ProfTitle对象
        Collection<ProfTitle> profTitles = new TreeSet<ProfTitle>();
        //准备预编译的语句
        int totalNum = CommonDao.getInstance().count(connection,"ProfTitle");
        //创建查询的主语句
        StringBuilder sql_select = new StringBuilder("SELECT * FROM ProfTitle ");
        //如果有条件，则生成条件子句，并附加到主语句后面
        if(conditionList!=null){
            String whereClause = Condition.toWhereClause(conditionList);
            sql_select.append(whereClause);
        }
        if(pagination!=null){
            String LimitClause = pagination.toLimitClause(totalNum);
            sql_select.append(LimitClause);
        }
        //执行预编译语句，结果集放在resultSet中
        PreparedStatement pstmt = connection.prepareStatement(sql_select.toString());
        ResultSet resultSet = pstmt.executeQuery();
        //遍历resultSet
        while(resultSet.next()){
            profTitles.add(new ProfTitle(resultSet.getInt("id"),
                    resultSet.getString("description"),
                    resultSet.getString("no"),
                    resultSet.getString("remarks")));
        }
        return profTitles;
    }

    public static ProfTitle find(int id,Connection connection) throws SQLException {

        //创建sql语句
        String find_sql = "select * from ProfTitle where id = ?";
        //在该连接上创建预编译语句对象
        PreparedStatement pstmt = connection.prepareStatement(find_sql);
        //为预编译参数赋值
        pstmt.setInt(1,id);
        //根据id查找
        ResultSet resultSet = pstmt.executeQuery();
        resultSet.next();
        ProfTitle profTitle = new ProfTitle(id,
                resultSet.getString("description"),
                resultSet.getString("no"),
                resultSet.getString("remarks"));
        return profTitle;
    }

    public static boolean update(ProfTitle profTitle,Connection connection) throws SQLException {


        //创建sql语句
        String updateProfTitle_sql = "update profTitle set description=?,no=?,remarks=? where id=?";
        //在该链接上创建预编译语句对象
        PreparedStatement pstmt = connection.prepareStatement(updateProfTitle_sql);
        //为预编译参数赋值
        pstmt.setInt(4,profTitle.getId());
        pstmt.setString(1,profTitle.getDescription());
        pstmt.setString(2,profTitle.getNo());
        pstmt.setString(3,profTitle.getRemarks());
        //根据执行结果，具体判断所要返回的值
        if (pstmt.executeUpdate()==0){
            return false;
        }else
            return  true;
    }

    public static boolean add(ProfTitle profTitle,Connection connection) throws SQLException {

        //创建sql语句
        String addProfTitle_sql =
                "insert into ProfTitle (description,no,remarks) values " +
                        "(?,?,?);";
        //在该链接上创建预编译语句对象
        PreparedStatement pstmt = connection.prepareStatement(addProfTitle_sql);
        //为预编译参数赋值

        pstmt.setString(1,profTitle.getDescription());
        pstmt.setString(2,profTitle.getNo());
        pstmt.setString(3,profTitle.getRemarks());
        //获取执行结果
        int affectedRowNum = pstmt.executeUpdate();
        //根据结果，具体判断所要返回的值
        if (affectedRowNum == 1) {
            return true;
        } else
            return false;
    }

    public static boolean delete(int id,Connection connection) throws SQLException{

        //创建sql语句
        String deleteProfTitle_sql = "DELETE FROM ProfTitle WHERE id=?";
        //在该连接上创建预编译语句对象
        PreparedStatement pstmt = connection.prepareStatement(deleteProfTitle_sql);
        //为预编译参数赋值
        pstmt.setInt(1,id);
        if (pstmt.executeLargeUpdate() == 1){
            return true;
        }else {
            return false;
        }
    }
    public static boolean delete(ProfTitle profTitle,Connection connection) throws SQLException {

        String deleteProfTitle_sql = "delete from ProfTitle where no = ?";
        //在连接上创建预编译语句对象
        PreparedStatement pstmt = connection.prepareStatement(deleteProfTitle_sql);
        //为预编译参数赋值
        pstmt.setString(1, profTitle.getNo());
        //判断是否删除成功
        if(pstmt.executeUpdate() == 0){
            return false;
        }else {
            return true;
        }
    }
}
